/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package entities;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.ArrayList;
import utils.ChocoMessages;

/**
 * The class used to store all the actions in a certain month
 * @author chocoseb
 */
public class Month {

    private String _name;
    private Year _year;
    private ArrayList<Action> _actions = new ArrayList<Action>();

    /**
     * Create a new Month
     * @param p_year
     * @param p_monthNumber From 0 to 11
     */
    public Month(Year p_year, int p_monthNumber) {
        _year = p_year;
        _name = Month.getMonthName(p_monthNumber);
        this.load();
    }
    
    public Month() {
    }

    public String getMonthName() {
        return _name;
    }

    /**
     * Get the path and filename for the file that will contains this month actions.
     * @return
     */
    public String getFileName() {
        return _year.getFolderName() + "\\" + _name + ".bdj";
    }

    /**
     * Delete an action at an index
     * @param index
     */
    public void deleteAtIndex(int index) {
        _actions.remove(index);
    }

    /**
     * Add an Action
     * @param index
     */
    public void add(Action a) {
        _actions.add(a);
    }

    /**
     * Save the month object into a file. The filename and path are decide with the getFileName method.
     */
    public void save() {
        try {
            DataOutputStream out = new DataOutputStream(new FileOutputStream(this.getFileName()));
            for (Action a : _actions) {
                a.save(out);
            }
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /**
     * Load the actions from the file.
     */
    private void load() {
        try {
            DataInputStream in = new DataInputStream(new FileInputStream(this.getFileName()));
            while (in.available() > 0) {
                _actions.add(new Action(this, in));
            }
            in.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public ArrayList<Action> getActions() {
        return _actions;
    }

    public double getMonthIncomes() {
        double total = 0.0;
        for (Action a : _actions) {
            if (a.getAmount() > 0.0) {
                total += a.getAmount();
            }
        }
        return total;
    }

    public double getMonthExpense() {
        double total = 0.0;
        for (Action a : _actions) {
            if (a.getAmount() < 0.0) {
                total += a.getAmount();
            }
        }
        if (total < 0){
            total = -total;
        }
        return total;
    }

    public double getMonthDeficit() {
        double total = 0.0;
        for (Action a : _actions) {
            total += a.getAmount();
        }
        return total;
    }

    /**
     * Return the name of a month with his month index
     * @param p_monthNumber Integer from 0 to 11 representing the month
     * @return
     */
    public static String getMonthName(int p_monthNumber) {
        return ChocoMessages.message("Month_" + String.valueOf(p_monthNumber));
    }
}
